/**
 * 字符串验证对象
 */
MPT.addObject('Verifier', function() {

    //---------------------通用验证-------------------

    /**
    * 判断参数是否为空
    */
    this.empty = function(_val) {
        var val = jQuery.trim(_val);
        if (val.length <= 0) return true;
        else return false;
    };

    /**
    * 只能输入数字
    */
    this.number = function(_val) {
        return /^[0-9]*$/.test(_val);
    }

    /**
    * 只能输入 n 位的数字
    */
    this.numCount = function(_val, _n) {
        return (new RegExp("^\\d{" + _n + "}$")).test(_val);
    };

    /**
    * 只能输入至少 n 位的数字
    */
    this.numMinCount = function(_val, _n) {
        return (new RegExp("^\\d{" + _n + ",}$")).test(_val);
    };

    /**
    * 只能输入 m ～ n 位的数字
    */
    this.numBetween = function(_val, _m, _n) {
        return (new RegExp("^\\d{" + _m + "," + _n + "}$")).test(_val);
    };

    /**
    * 只能输入零开头的数字
    */
    this.numFstZero = function(_val) {
        return (new RegExp("^0\\d*$")).test(_val);
    };

    /**
    * 只能输入有两位小数的正实数
    */
    this.price = function(_val) {
        return (new RegExp("^\\d+\.\\d{2}?$")).test(_val);
    };

    /**
    * 只能输入由 26 个英文字母组成的字符串
    */
    this.letter = function(_val) {
        return (new RegExp("^[A-Za-z]+$")).test(_val);
    };

    /**
    * 只能输入由 26 个大写英文字母组成的字符串
    */
    this.letterUpper = function(_val) {
        return (new RegExp("^[A-Z]+$")).test(_val);
    };

    /**
    * 只能输入由 26 个小写英文字母组成的字符串
    */
    this.letterLower = function(_val) {
        return (new RegExp("^[a-z]+$")).test(_val);
    };

    /**
    * 只能输入由数字和 26 个英文字母组成的字符串
    */
    this.letterAndNum = function(_val) {
        return (new RegExp("^[A-Za-z0-9]+$")).test(_val);
    };

    /**
    * 只能输入由数字、26个英文字母或者下划线组成的字符串
    */
    this.letterNumUnline = function(_val) {
        return (new RegExp("^\\w+$")).test(_val);
    };

    /**
    * 验证是否含有非法字符
    */
    this.specialChar = function(_val) {
        return (new RegExp("^(?:[\\u4e00-\\u9fa5]*\\w*\\s*)+$")).test(_val);
    };

    /**
    * 只能输入汉字
    */
    this.chinese = function(_val) {
        return (new RegExp("^[\\u4e00-\\u9fa5]{0,}$")).test(_val);
    };

    /**
    * 验证 Email 地址
    */
    this.email = function(_val) {
        return (new RegExp("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")).test(_val);
    };

    /**
    * 验证身份证
    */
    this.isCardID = function(pId) {
        var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
        pId = pId.toLowerCase();
        var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2];
        var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        var Checker = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1];
        if (pId.length != 15 && pId.length != 18)
            return "-2";
        var Ai = pId.length == 18 ? pId.substring(0, 17) : pId.slice(0, 6) + "19" + pId.slice(6, 16);
        if (!/^\d+$/.test(Ai))
            return "-3";
        if (aCity[parseInt(pId.substr(0, 2))] == null)
            return "-4";
        var yyyy = Ai.slice(6, 10), mm = Ai.slice(10, 12) - 1, dd = Ai.slice(12, 14);
        var d = new Date(yyyy, mm, dd), now = new Date();
        var year = d.getFullYear(), mon = d.getMonth(), day = d.getDate();
        if (year != yyyy || mon != mm || day != dd || d > now || year < 1940)
            return "-4";
        if (now < d.setFullYear(d.getFullYear() + 18)) {
            return "-5";
        }
        for (var i = 0, ret = 0; i < 17; i++)
            ret += Ai.charAt(i) * Wi[i];
        Ai += arrVerifyCode[ret %= 11];
        return pId.length == 18 && pId != Ai ? "-6" : true;
    };


    //----------------------------------------------------------------------------------------------



    /**
    * 判断字符串参数长度是否符合要求
    * 当_max未定义则判断长度是否等于_min
    */
    this.checkLength = function(_val, _min, _max) {
        var val = jQuery.trim(_val);
        if (_max) {
            if (val.length < _min || val.length > _max) return false;
            else return true;
        }
        else {
            if (val.length == _min) return true;
            else return false;
        }
    };

    /**
    * 判断字符串参数字节长度是否符合要求
    * 当_max未定义则判断长度是否等于_min
    */
    this.checkByteLen = function(_val, _min, _max) {
        var val = jQuery.trim(_val);
        if (_max) {
            if (val.byteLength() < _min || val.byteLength() > _max) return false;
            else return true;
        }
        else {
            if (val.byteLength() == _min) return true;
            else return false;
        }
    };

    /**
    * 判断字符串参数是否以数字、字母线组成
    */
    this.strTypeOne = function(_val) {
        var val = jQuery.trim(_val);
        //trace(!val.match(/^[a-zA-Z0-9_]+$/));
        if (!val.match(/^[a-zA-Z0-9_]+$/)) return false;
        else return true;
    };

    /**
    * 判断字符串中是否包含有特殊字符：返回true表示包含特殊字符  
    */
    this.containSpecial = function(_val) {
        var containSpecial = RegExp("^[\u4e00-\u9fa5a-zA-Z0-9_]+$");
        return (!containSpecial.test(_val));
    };


    // CharMode函数
    // 测试某个字符是属于哪一类.
    var CharMode = function(iN) {
        if (iN >= 48 && iN <= 57) // 数字
            return 1;
        if (iN >= 65 && iN <= 90) // 大写字母
            return 2;
        if (iN >= 97 && iN <= 122) // 小写
            return 4;
        else
            return 8; // 特殊字符
    };
    // bitTotal函数
    // 计算出当前密码当中一共有多少种模式
    var bitTotal = function(num) {
        modes = 0;
        for (i = 0; i < 4; i++) {
            if (num & 1)
                modes++;
            num >>>= 1;
        }
        return modes;
    };
    // checkStrong函数
    // 返回密码的强度级别
    var checkStrong = function(sPW) {
        if (sPW.length <= 4)
            return 0; // 密码太短
        Modes = 0;
        for (i = 0; i < sPW.length; i++) {
            // 测试每一个字符的类别并统计一共有多少种模式.
            Modes |= CharMode(sPW.charCodeAt(i));
        }
        return bitTotal(Modes);
    };

    this.PWDStrong = function(_str) {
        return checkStrong(_str);
    };

    this.isCardID = function(pId) {
        var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
        pId = pId.toLowerCase();
        var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2];
        var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        var Checker = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1];
        if (pId.length != 15 && pId.length != 18)
            return "-2";
        var Ai = pId.length == 18 ? pId.substring(0, 17) : pId.slice(0, 6) + "19" + pId.slice(6, 16);
        if (!/^\d+$/.test(Ai))
            return "-3";
        if (aCity[parseInt(pId.substr(0, 2))] == null)
            return "-4";
        var yyyy = Ai.slice(6, 10), mm = Ai.slice(10, 12) - 1, dd = Ai.slice(12, 14);
        var d = new Date(yyyy, mm, dd), now = new Date();
        var year = d.getFullYear(), mon = d.getMonth(), day = d.getDate();
        if (year != yyyy || mon != mm || day != dd || d > now || year < 1940)
            return "-4";
        if (now < d.setFullYear(d.getFullYear() + 18)) {
            return "-5";
        }
        for (var i = 0, ret = 0; i < 17; i++)
            ret += Ai.charAt(i) * Wi[i];
        Ai += arrVerifyCode[ret %= 11];
        return pId.length == 18 && pId != Ai ? "-6" : 1;
    };


    //---------------------特殊验证-------------------

    /**
    * 验证账号，只能由6-16位小写字母或数字组成
    */
    this.account = function(_val) {
        return (new RegExp("^[a-z0-9]{6,16}$")).test(_val);
    };

    /**
    * 验证校验码，只能由4位字母或数字组成
    */
    this.authcode = function(_val) {
        return (new RegExp("^[a-zA-Z0-9]{4}$")).test(_val);
    };

    /**
    * 验证昵称，最长为12个字符，由中英文数字组成(中文占2个字符)！
    */
    this.nickname = function(_val) {
        return (new RegExp("^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,12}$")).test(_val);
    };

    /**
    * 验证密码，以字母开头，长度在6～18之间，只能包含字符、数字和下划线
    */
    this.password = function(_val, _m, _n) {
        return (new RegExp("^[a-zA-Z]\\w{" + (_m || 5) + "," + (_n || 17) + "}$")).test(_val);
    };


});
